﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using NKH.MindSqualls;

namespace NXTRobotController
{
    public partial class frmLEGORobotSettings : Form
    {
        LEGORobotControl ctrl;
        SerialPort BluetoothConnection = new SerialPort();

        public bool UsingLEGO { get; set; }

        public frmLEGORobotSettings(ref LEGORobotControl control)
        {
            InitializeComponent();
            ctrl = control;
            comboBox1.SelectedIndex = 0;
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            if (!ctrl.IsConnected())
            {
                this.Cursor = Cursors.WaitCursor;
                byte comPort;
                if (byte.TryParse(txtComPort.Text, out comPort))
                {
                    try
                    {
                        ctrl.Connect(comPort);
                        if (ctrl.IsConnected())
                        {
                            btnConnect.Enabled = false;
                            lblNXTStatus.Text = "Connected";
                            lblNXTStatus.ForeColor = Color.Green;
                        }
                    }
                    catch (Exception exc)
                    {
                        MessageBox.Show(string.Format("Error: {0}", exc.Message));
                    }
                }
                else
                {
                    MessageBox.Show("Invalid COM Port");
                }
            }
            this.Cursor = Cursors.Default;
        }

        private void btnDisconnect_Click(object sender, EventArgs e)
        {
            if (ctrl.IsConnected())
            {
                ctrl.Disconnect();
                btnConnect.Enabled = true;
                lblNXTStatus.Text = "Disconected";
                lblNXTStatus.ForeColor = Color.Red;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            ctrl.DoAction(comboBox1.SelectedItem.ToString());
            button1.Enabled = true;
        }

        private void btnConnect_EnabledChanged(object sender, EventArgs e)
        {
            btnDisconnect.Enabled = !btnConnect.Enabled;
            txtComPort.Enabled = btnConnect.Enabled;
            panel2.Enabled = btnDisconnect.Enabled;
        }

        private void chkUsingLEGO_CheckedChanged(object sender, EventArgs e)
        {
            panel1.Enabled = chkUsingLEGO.Checked;
            this.UsingLEGO = chkUsingLEGO.Checked;
        }

        private void frmLEGORobotSettings_Shown(object sender, EventArgs e)
        {
            btnConnect.Enabled = !ctrl.IsConnected();
            chkUsingLEGO.Checked = UsingLEGO;
        }

    }
}
